package com.rabc.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.base.BaseController;

import com.rabc.web.entity.SysRole;
import com.rabc.web.entity.request.SysRoleMenReq;
import com.rabc.web.mapper.SysRoleMapper;
import com.rabc.web.service.SysRoleService;
import com.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Slf4j
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRolesService;

    @ApiOperation("查询角色列表")
    @PostMapping("/list")
    public R list( SysRole sysRole) {
        List<SysRole> list = sysRolesService.selectRolesList(sysRole);
        return R.success(list);
    }

    @ApiOperation("根据ID查询角色")
    @GetMapping("/{roleId}")
    public R getInfo(@PathVariable String roleId) {
        SysRole sysRoles = sysRolesService.getById(roleId);
        return R.success(sysRoles);
    }

    @ApiOperation("新增角色")
    @PostMapping("/add")
    public R add(@RequestBody SysRole sysRoles) {
        long count = sysRolesService.count(new QueryWrapper<SysRole>().eq("role_key", sysRoles.getRoleKey()));
        long roleKeyCount = sysRolesService.count(new QueryWrapper<SysRole>().eq("role_key", sysRoles.getRoleKey()));
        if (count>0){
            return R.error("当前角色已经存在");
        }else if (roleKeyCount>0){

            return R.error("新增角色' [" + sysRoles.getRoleName() + " ]'失败，角色权限已存在");
        }
        boolean save = sysRolesService.save(sysRoles);

        return save? R.success().data("checek","ces"):R.error("当前角色 ['" + sysRoles.getRoleName() + "] '新增失败，联系管理员处理");

    }

    @ApiOperation("修改角色")
    @PutMapping
    public R edit(@RequestBody SysRole sysRoles) {

        long count = sysRolesService.count(new QueryWrapper<SysRole>().eq("role_key", sysRoles.getRoleKey()));
        long roleKeyCount = sysRolesService.count(new QueryWrapper<SysRole>().eq("role_key", sysRoles.getRoleKey()));
        if (count>0){
            return R.error("当前角色已经存在");
        }else if (roleKeyCount>0){

            return R.error("新增角色' [" + sysRoles.getRoleName() + " ]'失败，角色权限已存在");
        }
        boolean result = sysRolesService.updateById(sysRoles);
        return  result?R.success():R.error();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete")
    public R remove(@RequestBody String[] ids) {

        boolean result = sysRolesService.removeBatchByIds(Arrays.asList(ids));
        return result ? R.success() : R.error();
    }

    @PostMapping("")
    public R  changeStatus(){
        return R.success();
    }

    @ApiOperation("分配权限")
    @PostMapping("/grantPermissions")
    public R grantPermissions(@RequestBody SysRoleMenReq roleMenu) {
        // 处理逻辑
        System.out.println("Received menu IDs: " + roleMenu);
        int i = sysRolesService.grantPermissions(roleMenu.getMenuIds(), roleMenu.getRoleId());
        return    i>0?R.success():R.error();
    }

   public R getOptionSelect(){
        return R.success();
   }




    @GetMapping("/permission/list")
    public R ist() {
        return buildComplexResponse();
    }


    @GetMapping("/list")
    public R alllist() {
        return buildResponse();
    }


    @GetMapping("/permission/{roleId}")
    public R permission(@PathVariable("roleId") String roleId) {

        List<String> data = Arrays.asList(
                "1",
                "1-1",
                "1-2",
                "1-3",
                "2",
                "2-1",
                "3",
                "4",
                "5"
        );
        return  R.success(data);
    }
    @PostMapping("/distribute-permission")
    public R distributePermission(){
        return R.success();
    }


    public static R buildResponse() {
        List<Map<String, Object>> data = new ArrayList<>();

        data.add(createRole("1", "超级管理员", "唯一账号，可以操作系统所有功能"));
        data.add(createRole("2", "管理员", "由超管指定，可以为多个，协助超管管理系统"));
        data.add(createRole("3", "人事经理", "主管人事相关业务"));
        data.add(createRole("4", "销售经理", "主管销售相关业务"));
        data.add(createRole("5", "保安队长", "主管安保相关业务"));
        data.add(createRole("6", "员工", "普通员工"));

        return R.success(data);
    }

    private static Map<String, Object> createRole(String id, String title, String describe) {
        Map<String, Object> role = new HashMap<>();
        role.put("id", id);
        role.put("title", title);
        role.put("describe", describe);
        return role;
    }

    public static R buildComplexResponse() {
        R response = R.success();

        List<Map<String, Object>> data = new ArrayList<>();

        // Constructing the first item
        Map<String, Object> item1 = new HashMap<>();
        item1.put("id", "1");
        item1.put("permissionName", "员工管理");
        item1.put("permissionMark", "userManage");
        item1.put("permissionDesc", "员工管理菜单");

        List<Map<String, Object>> children1 = new ArrayList<>();
        children1.add(createChild("1-1", "分配角色", "distributeRole", "为员工分配角色"));
        children1.add(createChild("1-2", "导入员工", "importUser", "通过 excel 导入员工"));
        children1.add(createChild("1-3", "删除员工", "removeUser", "删除员工"));
        item1.put("children", children1);

        data.add(item1);

        // Constructing the second item
        Map<String, Object> item2 = new HashMap<>();
        item2.put("id", "2");
        item2.put("permissionName", "角色列表");
        item2.put("permissionMark", "roleList");
        item2.put("permissionDesc", "角色列表菜单");

        List<Map<String, Object>> children2 = new ArrayList<>();
        children2.add(createChild("2-1", "分配权限", "distributePermission", "为角色分配权限"));
        item2.put("children", children2);

        data.add(item2);

        // Constructing the third item
        Map<String, Object> item3 = new HashMap<>();
        item3.put("id", "3");
        item3.put("permissionName", "权限列表");
        item3.put("permissionMark", "permissionList");
        item3.put("permissionDesc", "权限列表菜单");
        item3.put("children", new ArrayList<>());

        data.add(item3);

        // Constructing the fourth item
        Map<String, Object> item4 = new HashMap<>();
        item4.put("id", "4");
        item4.put("permissionName", "文章排名");
        item4.put("permissionMark", "articleRanking");
        item4.put("permissionDesc", "文章排名菜单");
        item4.put("children", new ArrayList<>());

        data.add(item4);

        // Constructing the fifth item
        Map<String, Object> item5 = new HashMap<>();
        item5.put("id", "5");
        item5.put("permissionName", "创建文章");
        item5.put("permissionMark", "articleCreate");
        item5.put("permissionDesc", "创建文章页面");
        item5.put("children", new ArrayList<>());

        data.add(item5);

        return R.success(data);
    }

    private static Map<String, Object> createChild(String id, String permissionName, String permissionMark, String permissionDesc) {
        Map<String, Object> child = new HashMap<>();
        child.put("id", id);
        child.put("permissionName", permissionName);
        child.put("permissionMark", permissionMark);
        child.put("permissionDesc", permissionDesc);
     return  child;
    }
}
